package cn.ckx.coding.moudle.user.queue;

import cn.ckx.coding.moudle.user.config.RabbitMqDelayConfig;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @ClassName QuesDelayProducer
 * @Description 生产者
 * @Author c
 * @Date 2020/12/25
 * @Version 1.0
 **/
@Component
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class QuesProducer {

    private final RabbitTemplate rabbitTemplate;

    public void sendTest1(String id, int ttl){
        rabbitTemplate.convertAndSend(RabbitMqDelayConfig.DELAY_EXCHANGE,RabbitMqDelayConfig.DELAY_ROUTING_KEY,id, message -> {
            message.getMessageProperties().setDelay(ttl);
            return message;
        });
        log.info("延迟队列测试发送1: id-{},发送时间-{},过期时长-{}s",id,LocalDateTime.now(),ttl/1000);
    }

    public void sendTest2(String id, int ttl){
        rabbitTemplate.convertAndSend(RabbitMqDelayConfig.DELAY_EXCHANGE,RabbitMqDelayConfig.DELAY_ROUTING_KEY_2,id, message -> {
            message.getMessageProperties().setDelay(ttl);
            return message;
        });
        log.info("延迟队列测试发送2: id-{},发送时间-{},过期时长-{}s",id,LocalDateTime.now(),ttl/1000);
    }
}
